package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;


//    @Override
//    public List<Setmeal> getSetmealByCategoryId(Long categoryId) {
//        List<Setmeal> setmeals=setMealMapper.getSetmealByCategoryId(categoryId);
//        return setmeals;
//    }
    //todo 这个方法和getSetmealWithDishesById
    @Override
    public List<DishItemVO> getDishItemBySetmealId(Long id) {
        return List.of();
    }

    @Override
    public List<Setmeal> getSetmealsByCondition(Setmeal setmeal) {
        List<Setmeal> setmeals=setMealMapper.getSetmealsByCondition(setmeal);
        return setmeals;
    }

    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal saveSetmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, saveSetmeal);
        //避免添加的这个套餐中最初含有未起售的菜品，而出现冲突
        saveSetmeal.setStatus(StatusConstant.DISABLE);

        setMealMapper.insert(saveSetmeal);

        Long setmealId = saveSetmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setMealDishMapper.insertBatch(setmealDishes);

    }

    //todo 优化查询方案
    // 1、更改查询方法     2、将下面的查询改为并发查询
    @Override
    public SetmealVO getSetmealWithDishesById(Long id) {
        Setmeal setmeal= setMealMapper.getById(id);

        SetmealDish setmealDish = new SetmealDish();
        setmealDish.setSetmealId(setmeal.getId());
        List<SetmealDish> dishes = setMealDishMapper.getByCondition(setmealDish);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(dishes);

        return setmealVO;
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO query) {
        PageHelper.startPage(query.getPage(),query.getPageSize());

        Page<SetmealVO>setmealVOS=setMealMapper.pageQuerySetmealWithCategory(query);

        return new PageResult(setmealVOS.getTotal(),setmealVOS.getResult());
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        if(status == StatusConstant.ENABLE){
            List<Dish> dishBySetmealId = dishMapper.getDishBySetmealId(id);
            if(dishBySetmealId!=null && !dishBySetmealId.isEmpty()) {
                for (Dish dish : dishBySetmealId) {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }
        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setMealMapper.updateById(setmeal);

    }


    @Transactional
    public void deleteSetmealWithDishByIds(List<Long> ids) {
        setMealMapper.deleteByIds(ids);
        setMealDishMapper.deleteBySetmealIds(ids);
    }

    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改setmeal表
        setMealMapper.updateById(setmeal);

        //删除这个套餐原本的dish
        setMealDishMapper.deleteBySetmealIds(List.of(setmeal.getId()));

        //插入这个套餐的dish
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setMealDishMapper.insertBatch(setmealDishes);
    }


}
